#!/bin/bash
##定义字体效果
#红色背景
red_bg='\e[41;33;1m'
#红色字体
red_zt='\e[1;31m'
#绿色背景
green_bg='\033[0;32;1m'
#绿色字体
green_zt='\e[1;32m'
#关闭效果
end='\033[0m'
#字体闪烁
ss='\033[05m'
#消隐
sy='\33[08m'
##常用语句定义区
print_green_bg_red_zt(){
	echo -e "${green_bg}${ss}${green_bg}正在进入...${end}${end}${end}\n${red_zt}${1}...${end}"
}
print_red_z_green_z_info(){
	echo -e "${red_zt}${1}:${end}${green_zt}${2}${end}"
}
cheks(){
	echo -e "${red_zt}正在检测${1}...${end}"
}
print_green_zt(){
	echo -e "${green_zt}${1}${end}"
}
print_red_z_green_z(){
	echo -e "${red_zt}${1}${end}${green_zt}${2}${end}${green_zt}${3}${end}"
}
print_red_z(){
	echo -e "${red_zt}${1}${end}"
}
print_red_z_b(){
	echo -e "${red_bg}${1}${end}"
}
print_red_z_z_bg(){
	echo -e "${red_zt}${1}${end}${red_bg}${2}${end}"
}
input(){
	echo -e "${red_zt}${1}${end}"
	read $2
    if [[ -z "$2" ]];then
        print_red_z_b "$1" "未输入"
    fi
}
info='编写时间：2020-8-20
By：不愿意透漏姓名的男神
My_Gitee_Page：https://gitee.com/faith01238/
My_CSDN_Page：https://blog.csdn.net/qq_36154886
My_cnblogs_Page：https://www.cnblogs.com/liuyi778'
for i in $info;do
    print_green_zt "$i"
done
service_install(){
    for i in $*;do
        print_red_z_green_z_info "正在安装---->>>>" "${i}\t安装速度根据包的大小及网络而定,请耐心等待"
        print_red_z_green_z_info "当前脚本进程PID---->>>" "$$"
        $install_s $i
        chek_status "$?" "${i}安装"
    done
}
service_start(){
    systemctl daemon-reload
    for i in $*;do
        systemctl restart $i
        chek_status "$?" "${i}服务启动"
        systemctl enable $i > /dev/null
        chek_status "$?" "${i}设置服务开机启动"
    done
}
chek_status(){
    if [[ "$1" -eq '0' ]];then
        print_red_z_green_z_info "$2" "成功"
        return 0
    else
        status='true'
        while $status;do
            print_red_z_z_bg "$2" "失败"
            err='n'
            input "是否停止脚本运行[当前脚本PID: $$]\n输入f忽略该提示" "err"
            if [[ "$err" == 'f' ]];then
                status='false'
                if [[ "$status" == 'false' ]];then
                    print_red_z "已跳过该错误:[${2}失败]"
                else
                    status='true'
                fi
            else
                status=true
            fi
        done
    fi
}
info_input(){
    input "请输入你的IP地址" "IP"
    input "请输入你的网段[10.10.10]" "subnet"
    input "请输入子网掩码[255.255.255.0]" "mask"
    input "请输入dhcp地址池起点[${subnet}.100]" "start"
    input "请输入dhcp地址池终点[${subnet}.200]" "end"
    input "请输入网关IP[${subnet}.254]" "gateway"
    input "请输入域名[网关或者DNS服务器]" "server_name"
}
server_installs(){}
    rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
    se='install cobbler cobbler-web dhcp tftp pykickstart httpd pykickstart fence-agents'
    service_install "$se"
}
dhcp_config(){
    echo -e "ddns-update-style interim;

allow booting;
allow bootp;

ignore client-updates;
set vendorclass = option vendor-class-identifier;

option pxe-system-type code 93 = unsigned integer 16;

subnet ${subnet}.0 netmask ${mask} {
     option routers             ${gateway};
     option domain-name-servers ${server_name};
     option subnet-mask         ${mask};
     range dynamic-bootp        ${start} ${end};
     default-lease-time         21600;
     max-lease-time             43200;
     next-server                \$next_server;
     class "pxeclients" {
          match if substring (option vendor-class-identifier, 0, 9) = "PXEClient";
          if option pxe-system-type = 00:02 {
                  filename "ia64/elilo.efi";
          } else if option pxe-system-type = 00:06 {
                  filename "grub/grub-x86.efi";
          } else if option pxe-system-type = 00:07 {
                  filename "grub/grub-x86_64.efi";
          } else if option pxe-system-type = 00:09 {
                  filename "grub/grub-x86_64.efi";
          } else {
                  filename "pxelinux.0";
          }
     }

}

group {
        #for mac in $dhcp_tags[$dhcp_tag].keys():
            #set iface = $dhcp_tags[$dhcp_tag][$mac]
    host $iface.name {
        #if $iface.interface_type == "infiniband":
        option dhcp-client-identifier = $mac;
        #else
        hardware ethernet $mac;
        #end if
        #if $iface.ip_address:
        fixed-address $iface.ip_address;
        #end if
        #if $iface.hostname:
        option host-name "\$iface.hostname";
        #end if
        #if $iface.netmask:
        option subnet-mask $iface.netmask;
        #end if
        #if $iface.gateway:
        option routers $iface.gateway;
        #end if
        #if $iface.enable_gpxe:
        if exists user-class and option user-class = "gPXE" {
            filename "http://$cobbler_server/cblr/svc/op/gpxe/system/\$iface.owner";
        } else if exists user-class and option user-class = "iPXE" {
            filename "http://$cobbler_server/cblr/svc/op/gpxe/system/\$iface.owner";
        } else {
            filename "undionly.kpxe";
        }
        #else
        filename "$iface.filename";
        #end if
        ## Cobbler defaults to $next_server, but some users
        ## may like to use $iface.system.server for proxied setups
        next-server $next_server;
        ## next-server $iface.next_server;
    }
        #end for
}
#end for" 
}
service_config(){
    if [[ -f "/etc/selinux/config" ]];then
            sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config
    fi
    if [[ -f "/etc/sysconfig/selinux" ]];then
            sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux
    fi
    sed -i "s/server: 127.0.0.1/server: $IP/" /etc/cobbler/settings
    sed -i 's/manage_dhcp: 0/manage_dhcp: 1/' /etc/cobbler/settings
    sed -i 's/pxe_just_once: 0/pxe_just_once: 1/' /etc/cobbler/settings
    i=$(openssl passwd -1 -salt 'cobbler' '123456')
    o="default_password_crypted: \""$i".\""
    sed -i "s#default_password_crypted.*#$o#g" /etc/cobbler/settings
    sed -i "#${subnet}#$subnet#g" /etc/cobbler/settings



    sed  -i 's/disable.*/disable\t\t\t= no/' /etc/xinetd.d/tftp
    sed -i "s/#ServerName.*/ServerName $IP/g" /etc/httpd/conf/httpd.conf 
   

}' > 

se='httpd cobblerd rsyncd.service'
service_start "$se"
cobbler get-loaders
